<template>
  <div class="novel-item">
    <el-table :data="user" border style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column property="title" label="小说名称" width="120" />
      <el-table-column property="author" label="作者" width="120" />
      <el-table-column property="totalChapter" label="章节数" width="120" />
      <el-table-column property="isEnd" label="更新状态" width="120" />
      <el-table-column property="updateTime" label="最近更新时间" width="120" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      background
      :page-count="pages"
      :current-page="page"
      :page-size="size"
      layout="prev, pager, next, jumper"
      @current-change="updateCurrentPage"
    />
  </div>
</template>

<script>
import { getNovelList } from "@/api/novel";

export default {
  data() {
    return {
      title: "",
      novelKindId: "",
      isEnd: "",
      author: "",
      page: 1,
      size: 10,
      user: [],
      total: 0,
      pages: 0,
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    async getNovelList() {
      const res = await getNovelList(
        this.title,
        this.novelKindId,
        this.isEnd,
        this.author,
        this.page,
        this.size
      );
      if (res.data.success) {
        this.user = res.data.result.records;
        this.total = res.data.result.total;
        this.pages = res.data.result.pages;
      }
    },
    updateCurrentPage(page) {
      this.page = page;
      this.getNovelList();
    },
  },
  created() {
    this.getNovelList();
  },
};
</script>

<style>
.novel-item-name {
  margin: 10px 20px;
}
.pagination {
  display: flex;
  justify-content: center;
  margin: 10px 20px;
}
</style>